package com.heima.article.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.dto.CollectionBehaviorDto;
import com.heima.article.dto.EntryAndTypeDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApBehaviorEntry;
import com.heima.article.entity.ApCollection;
import com.heima.article.feign.ApBehaviorEntryFeign;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.IApArticleService;
import com.heima.article.service.IApCollectionService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP收藏信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-01-19
 */
@Service
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements IApCollectionService {

    @Autowired
    ApBehaviorEntryFeign behaviorEntryFeign;

    @Autowired
    private IApArticleService apArticleService;

    //保存收藏数据
    @Override
    public ResponseResult getCollection(CollectionBehaviorDto dto) {
        //1 获取用户ID
        User user = AppThreadLocalUtil.get();

        //2 用户id不为0
        if (user != null) {
            //2.1 查询收藏数据
            //2.1 封装用户id和类型type
            EntryAndTypeDto entryAndTypeDto = new EntryAndTypeDto();

            entryAndTypeDto.setEntryId(user.getUserId().toString());

            entryAndTypeDto.setType(1);
            //2.2 查询收藏数据
            ApBehaviorEntry entry = behaviorEntryFeign.findEntry(entryAndTypeDto);

            LambdaQueryWrapper<ApCollection> query = new LambdaQueryWrapper<>();

            ApCollection apCollection = new ApCollection();

            apCollection.setEntryId(entry.getId());

            apCollection.setArticleId(dto.getArticleId());

            apCollection.setCollectionTime(new Date());

            this.save(apCollection);
            //将收藏的数量添加到文章中
            Long articleId = dto.getArticleId();

            ApArticle one = apArticleService.getById(articleId);

            Integer collection = one.getCollection();

            int integer = ++collection;

            one.setCollection(integer);

            apArticleService.updateById(one);
            //响应数据
            return ResponseResult.okResult();
        }
        //3 设备id不为空
        if (dto.getEquipmentId() != null) {
            //3.1 封装设备id和类型type
            EntryAndTypeDto entryAndTypeDto = new EntryAndTypeDto();

            entryAndTypeDto.setEntryId(dto.getEquipmentId());

            entryAndTypeDto.setType(0);
            //3.2 查询收藏数据
            ApBehaviorEntry entry = behaviorEntryFeign.findEntry(entryAndTypeDto);

            ApCollection apCollection = new ApCollection();

            apCollection.setEntryId(entry.getId());

            apCollection.setArticleId(dto.getArticleId());

            apCollection.setCollectionTime(new Date());

            this.save(apCollection);

            //将收藏的数量添加到文章中
            Long articleId = dto.getArticleId();

            ApArticle one = apArticleService.getById(articleId);

            Integer collection = one.getCollection();

            int integer = ++collection;

            one.setCollection(integer);

            apArticleService.updateById(one);

            return ResponseResult.okResult();
        }
        return null;
    }

    //删除收藏数据
    @Override
    public ResponseResult delelteCollection(CollectionBehaviorDto dto) {

        //1 获取用户ID
        User user = AppThreadLocalUtil.get();

        //2 用户id不为空
        if (user != null) {
            //2.1 封装用户id和类型type
            EntryAndTypeDto entryAndTypeDto = new EntryAndTypeDto();

            entryAndTypeDto.setEntryId(user.getUserId().toString());

            entryAndTypeDto.setType(1);
            //2.2 删除收藏数据
            ApBehaviorEntry entry = behaviorEntryFeign.findEntry(entryAndTypeDto);

            LambdaQueryWrapper<ApCollection> query = new LambdaQueryWrapper<>();

            query.eq(ApCollection::getEntryId, entry.getId()).eq(ApCollection::getArticleId, dto.getArticleId());

            this.remove(query);
            //给文章中收藏数量-1
            Long articleId = dto.getArticleId();

            ApArticle one = apArticleService.getById(articleId);

            Integer collection = one.getCollection();

            int integer = --collection;

            one.setCollection(integer);

            apArticleService.updateById(one);
            //返回响应数据
            return ResponseResult.okResult();
        }
        //3 设备id不为空
        if (dto.getEquipmentId() != null) {
            //3.1 封装设备id和类型type
            EntryAndTypeDto entryAndTypeDto = new EntryAndTypeDto();

            entryAndTypeDto.setEntryId(dto.getEquipmentId());

            entryAndTypeDto.setType(0);
            //3.2 删除数据
            ApBehaviorEntry entry = behaviorEntryFeign.findEntry(entryAndTypeDto);

            LambdaQueryWrapper<ApCollection> query = new LambdaQueryWrapper<>();

            query.eq(ApCollection::getEntryId, entry.getId()).eq(ApCollection::getArticleId, dto.getArticleId());

            this.remove(query);
            //给文章中收藏数量-1
            Long articleId = dto.getArticleId();

            ApArticle one = apArticleService.getById(articleId);

            Integer collection = one.getCollection();

            int integer = --collection;

            one.setCollection(integer);

            apArticleService.updateById(one);
            //返回响应数据
            return ResponseResult.okResult();
        }
        return null;
    }
}
